let util = require('util');
let mysqlPool = require('../dao/MySQLPool');
const field = ["id", "clubId", "clubCoin", "unionCoin", "ownerUid", "ownerName", "clubName", "headimgurl", "createTick", "memberUid", "agree", "joinTick",
               "memberImgurl", "memberName", "setting", "gameType", "typeName", "create_time", "update_time", "join_time", "coins", "buffer"];

const fieldAgent = ["id", "clubId", "clubCoin", "unionCoin", "ownerUid", "ownerName", "clubName", "headimgurl", "createTick", "memberUid", "agree", "joinTick",
    "memberImgurl", "memberName", "setting", "gameType", "typeName", "create_time", "update_time", "join_time", "coins", "buffer", "agentLevel", "agentLeader", "agentJoinTime"];
class ClubMemberDao {
    constructor() {
    }

    async saveClubWithTx(club, conn){
        let sql1 = 'insert into club (';
        let sql2 = ') values (';
        let sql3 = ')';
        let args = [];
        for(let i = 0; i< field.length; i++){
            let key = field[i];
            if(club[key] !==null && club[key] !== undefined){
                sql1 = sql1  + field[i] + ", ";
                sql2 = sql2 + "?, "
                args.push(club[key]);
            }
        }
        sql1 = sql1.substring(0, sql1.length-2);
        sql2 = sql2.substring(0, sql2.length-2);
        let res;
        if(conn !== undefined){
            res = await conn.query(sql1 + sql2 + sql3, args);
        }else{
            res = await mysqlPool.query(sql1 + sql2 + sql3, args);
        }
        if (res){
            return res.insertId;
        }
        return null;
    }

    async saveClubWithTxAgent(club){
        let sql1 = 'insert into club (';
        let sql2 = ') values (';
        let sql3 = ')';
        let args = [];
        for(let i = 0; i< fieldAgent.length; i++){
            let key = fieldAgent[i];
            if(club[key] !==null && club[key] !== undefined){
                sql1 = sql1  + fieldAgent[i] + ", ";
                sql2 = sql2 + "?, "
                args.push(club[key]);
            }
        }
        sql1 = sql1.substring(0, sql1.length-2);
        sql2 = sql2.substring(0, sql2.length-2);

        let res = await mysqlPool.query(sql1 + sql2 + sql3, args);
        if (res){
            return res.insertId;
        }
        return null;
    }

    /**
     * 
     * @param {String} uid 
     * @param {String} clubID 
     */
    async getByUID(uid, clubID){
        let sql = 'select * from club where clubId = ? and memberUid = ? and agree = 1 ';
        let args = [clubID, uid];
        let res = await mysqlPool.query(sql, args);
        if (res){
            return res[0];
        }
    
        return null;
    }

    async getByUIDJoin(clubID){
        let sql = 'select * from club where clubId = ? and agree = 1 ';
        let args = [clubID];
        let res = await mysqlPool.query(sql, args);
        if (res){
            return res[0];
        }

        return null;
    }

    /**
     *
     * @param {String} uid
     * @param {String} clubID
     */
    async getByClubIdAndUid(uid, clubID){
        let sql = 'select * from club where clubId = ? and memberUid = ? ';
        let args = [clubID, uid];
        let res = await mysqlPool.query(sql, args);
        if (res && res.length > 0){
            return res[0];
        }
        return null;
    }

    /**
     *
     * @param {String} uid
     * @param {String} clubID
     */
    async getUnionByUID(uid,clubID,unionID){

        let sql = 'select club.* from club INNER JOIN clubinfo ON club.clubId = clubinfo.clubId ' +
            'where club.memberUid = ? and club.agree = 1 and clubinfo.unionId = ? and clubinfo.clubId = ? and clubinfo.rel = 0'
        let args = [uid, unionID, clubID];
        let res = await mysqlPool.query(sql, args);
        if (res && res.length > 0){
            return res[0];
        }

        return null;
    }

    //联盟冻结用户
    async getFrostByUID(uid,unionID){

        let sql = 'select * from union_frost where uId = ? and unionId = ?'
        let args = [uid, unionID];
        let res = await mysqlPool.query(sql, args);
        if (res && res.length > 0){
            return true;
        }

        return false;
    }

    //获取有足够联盟币的俱乐部
    async getEnoughUnionByUID(uid,unionID,coins){

        let sql = 'select club.* from club ' +
            'INNER JOIN clubinfo ON club.clubId = clubinfo.clubId ' +
            'where club.memberUid = ? and club.agree = 1 and clubinfo.unionId = ? and club.unionCoin >= ? and clubinfo.rel = 0'
        let args = [uid, unionID, coins];
        let res = await mysqlPool.query(sql, args);
        if (res && res.length > 0){
            return res[0];
        }

        return null;
    }

    async getUnionByUID2(uid,clubID,unionID){
        let sql = 'select club.* from club INNER JOIN clubinfo ON club.clubId = clubinfo.clubId where club.memberUid = ? and club.agree = 1 and clubinfo.unionId = ? and clubinfo.clubId = ?'
        let args = [uid, unionID, clubID];
        let res = await mysqlPool.query(sql, args);
        if (res){
            return res[0];
        }

        return null;
    }

    /**
     * @param {String} uid
     * @param {String} clubID
     * @param {Number} coins
     */
    async upateCoins(uid, clubID, coins){
        let sql = 'update club set coins = (coins + ?) where clubID = ? and memberUid = ?';
        let args = [coins, clubID, uid];
        let res = await mysqlPool.query(sql, args);
        if (res){
            return res.affectedRows;
        }
    
        return 0;
    }

    //联盟加减
    async upateUnionCoins(uid, clubID, coins){
        let sql = 'update club set unionCoin = (unionCoin + ?) where clubID = ? and memberUid = ?';
        let args = [coins, clubID, uid];
        let res = await mysqlPool.query(sql, args);
        if (res){
            return res.affectedRows;
        }

        return 0;
    }

     /**
     * @param {String} uid
     * @param {String} clubID
     * @param {Number} coins
     */
    async upateBuffer(uid, clubID, buffer){
        let sql = 'update club set buffer = (buffer + ?) where clubID = ? and memberUid = ?';
        let args = [buffer, clubID, uid];
        let res = await mysqlPool.query(sql, args);
        if (res){
            return res.affectedRows;
        }
    
        return 0;
    }

    async updateAgree(clubId, uid, agree){
        let sql = 'update club set agree = ?  where clubID = ? and memberUid = ?';
        let args = [agree, clubId, uid];
        let res = await mysqlPool.query(sql, args);
        if (res){
            return res.affectedRows;
        }
    }

    async quitClub(uid, clubId){
        let sql = 'update club set agree = -2, unionCoin = 0 where memberUid = ? and clubId = ?';
        let args = [uid, clubId];
        let res = await mysqlPool.query(sql, args);
    }


}

let clubMemberDao = new ClubMemberDao();
module.exports = clubMemberDao;